package org.example.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.example.domain.po.User;
import org.example.domain.po.UserTicket;
import org.example.mapper.UserTicketMapper;
import org.example.service.IUserTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yu
 * @since 2024-07-14
 */
@Service
public class UserTicketServiceImpl extends ServiceImpl<UserTicketMapper, UserTicket> implements IUserTicketService {


    @Override
    public List<UserTicket> getList1(Integer userId) {
        return lambdaQuery()
                .eq(UserTicket::getUserId,userId)
                .eq(UserTicket::getStatus,1)
                .list();
    }

    @Override
    public List<UserTicket> getList0(Integer userId) {
        return lambdaQuery()
                .eq(UserTicket::getUserId,userId)
                .eq(UserTicket::getStatus,0)
                .list();
    }

    @Override
    public void buyById(Integer id) {
        lambdaUpdate()
                .eq(UserTicket::getId,id)
                .set(UserTicket::getStatus,1)
                        .update();
        UserTicket userTicket = getById(id);
        User user = Db.lambdaQuery(User.class)
                        .eq(User::getId,userTicket.getUserId())
                                .one();
        Db.lambdaUpdate(User.class)
                .eq(User::getId,userTicket.getUserId())
                .set(User::getMoney,user.getMoney().subtract(userTicket.getPrice()))
                .update();

    }
}
